<?php
/**
 * 阵容
 */
namespace app\sports\match\service;

use app\sports\match\model\MatchModel;

class BattleService
{
    /**
     * 根据比赛ID获取对战信息
     */
    public function getBattleInfo($matchId)
    {
        $matchModel = new MatchModel();
        $matchInfo = $matchModel->getMatchInfoByMatchId($matchId);
        if (empty($matchInfo)) {
            return [];
        }

        //获取比赛时间
        $begin = date('Y年m月d H:i', $matchInfo['begin']);

        $leftTeamId = $matchInfo['left_team_id'];
        $rightTeamId = $matchInfo['right_team_id'];

        //获取球队名字 logo
        $leftTeamInfo = $matchModel->getTeamInfoByTeamId($leftTeamId);
        $leftTeamName = isset($leftTeamInfo['name']) ? $leftTeamInfo['name'] : '';
        $leftTeamLogo = isset($leftTeamInfo['logo']) ? $leftTeamInfo['logo'] : '';

        $rightTeamInfo = $matchModel->getTeamInfoByTeamId($rightTeamId);
        $rightTeamName = isset($rightTeamInfo['name']) ? $rightTeamInfo['name'] : '';
        $rightTeamLogo = isset($rightTeamInfo['logo']) ? $rightTeamInfo['logo'] : '';

        //获取开赛状态
        if (in_array($matchInfo['status'], [1,2,3,4,5,6,7,8])) {
            $status = ceil((time() - $matchInfo['begin']) / 60);
            if ($status < 0) {
                $status = '未';
            } else {
                //判断下是否已经结束
                $finish = $matchModel->getIncidentsByMatchId($matchId, MatchModel::TYPE_TWELVE);
                if ($finish) {
                    $status = '完';
                }
            }
        } else {
            $status = '异常';
        }

        return [
            'begin' => $begin,
            'left_team_name' => $leftTeamName,
            'left_team_logo' => $leftTeamLogo,
            'right_team_name' => $rightTeamName,
            'right_team_logo' => $rightTeamLogo,
            'status' => $status
        ];
    }

    /**
     * 根据比赛ID获取阵容
     */
    public function getBattleByMatchId($matchId)
    {
        //比赛通用信息
        $matchCommon = $this->getBattleInfo($matchId);

        $matchModel = new MatchModel();
        $matchInfo = $matchModel->getMatchInfoByMatchId($matchId);
        if (empty($matchInfo)) {
            return [];
        }

        $leftTeamId = $matchInfo['left_team_id'];
        $rightTeamId = $matchInfo['right_team_id'];

        $leftTeamInfo = $matchModel->getTeamInfoByTeamId($leftTeamId);

        //教练
        $leftTeamManager = isset($leftTeamInfo['manager']) ? $leftTeamInfo['manager'] : '';
        $leftTeamManager = json_decode($leftTeamManager, true);

        $leftTeamManagerName = isset($leftTeamManager['name_zh']) ? $leftTeamManager['name_zh'] : '';

        $rightTeamInfo = $matchModel->getTeamInfoByTeamId($rightTeamId);

        //教练
        $rightTeamManager = isset($rightTeamInfo['manager']) ? $rightTeamInfo['manager'] : '';
        $rightTeamManager = json_decode($rightTeamManager, true);
        $rightTeamManagerName = isset($rightTeamManager['name_zh']) ? $rightTeamManager['name_zh'] : '';

        //获取赛事信息
        $eventId = $matchInfo['event_id'];
        $eventInfo = $matchModel->getEventInfoById($eventId);
        $eventName = isset($eventInfo['name_zh']) ? $eventInfo['name_zh'] : '';

        //阵容
        $lineUp = $matchModel->getLineUpByMatchId($matchId);
        $lineUpLeftTeam = isset($lineUp['left_team']) ? $lineUp['left_team'] : '';
        $lineUpRightTeam = isset($lineUp['right_team']) ? $lineUp['right_team'] : '';

        //阵容详情
        $lineUpDetail = $matchModel->getLineUpDetail($matchId);
        $lineUpDetailRes = [];
        foreach ($lineUpDetail as $value) {
            if ($value['type'] == 1) {
                $lineUpDetailRes['home'][] = $value;
            }

            if ($value['type'] == 2) {
                $lineUpDetailRes['away'][] = $value;
            }
        }

        //本场换人
        $incidentsInfo = $matchModel->getIncidentsByMatchId($matchId, MatchModel::TYPE_NINE);
        $incidentsRes = [];
        $playerIdArr = [];
        foreach ($incidentsInfo as $value) {
            $incident = json_decode($value['incidents'], true);
            if (empty($incident) || empty($incident['in_player_id']) || empty($incident['out_player_id'])) {
                continue;
            }

            $info = [
                'time' => $incident['time'],
                'in_player_id' => $incident['in_player_id'],
                'in_player_name' => $incident['in_player_name'],
                'out_player_id' => $incident['out_player_id'],
                'out_player_name' => $incident['out_player_name']
            ];

            if ($incident['position'] == MatchModel::POSITION_HOME) {
                $incidentsRes['home'][] = $info;
            }

            if ($incident['position'] == MatchModel::POSITION_AWAY) {
                $incidentsRes['away'][] = $info;
            }

            $playerIdArr[] = $incident['in_player_id'];
            $playerIdArr[] = $incident['out_player_id'];
        }

        //获取球员名字
        $playerInfo = [];
        if ($playerIdArr) {
            $playerInfo = $matchModel->getPlayerInfoByIdArr($playerIdArr);
        }

        if (isset($incidentsRes['home'])) {
            foreach ($incidentsRes['home'] as &$value) {
                $value['in_player_logo'] = '';
                if (isset($playerInfo[$value['in_player_id']])) {
                    $value['in_player_logo'] = $playerInfo[$value['in_player_id']]['logo'];
                }

                $value['out_player_logo'] = '';
                if (isset($playerInfo[$value['out_player_id']])) {
                    $value['out_player_logo'] = $playerInfo[$value['out_player_id']]['logo'];
                }
            }
        }

        if (isset($incidentsRes['away'])) {
            foreach ($incidentsRes['away'] as &$value) {
                $value['in_player_logo'] = '';
                if (isset($playerInfo[$value['in_player_id']])) {
                    $value['in_player_logo'] = $playerInfo[$value['in_player_id']]['logo'];
                }

                $value['out_player_logo'] = '';
                if (isset($playerInfo[$value['out_player_id']])) {
                    $value['out_player_logo'] = $playerInfo[$value['out_player_id']]['logo'];
                }
            }
        }

        return [
            'match_common' => $matchCommon,
            'left_team_manager_name' => $leftTeamManagerName,
            'right_team_manager_name' => $rightTeamManagerName,
            'line_up_left_team' => $lineUpLeftTeam,
            'line_up_right_team' => $lineUpRightTeam,
            'event_name' => $eventName,
            'incidents' => $incidentsRes,
            'line_up_detail' => $lineUpDetailRes
        ];
    }

    /**
     * 获取比赛详情
     */
    public function getBattleDetailByMatchId($matchId)
    {
        //比赛通用信息
        $matchCommon = $this->getBattleInfo($matchId);

        //


        return [
            'match_common' => $matchCommon,
            'incidents' => $incidentsRes
        ];
    }
}